<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="title">
        <img id="logo" src="../assets/logo.gif" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 左侧 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleClick">|||</div>
        <el-menu
          router
          :unique-opened=true
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#369df8"
          

          :collapse="isCollapse"
          :collapse-transition=false

          
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
                @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-menu-item index="2">
            <i class="el-icon-user"></i>
            <span slot="title">导航二</span>
          </el-menu-item>
          <el-menu-item index="3">
            <i class="el-icon-setting"></i>
            <span slot="title">导航三</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 主要内容 -->
      <el-main>
        <div>
            <!-- 路由占位符 -->
          <router-view></router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {

  data() {
    return {
      //左侧菜单数据
      menuList: [],
      iconsObj: {
        '125': "iconfont icon-Customermanagement-fill",
        '103': "iconfont icon-product1",
        '101': "iconfont icon-Newuserzone-fill",
        '102': "iconfont icon-order-fill",
        '145': "iconfont icon-inspection-fill",
      },
      //是否折叠左侧
      isCollapse: false,
      //被激活的地址
      activePath: ''
    };
  },

  created() {
    
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')

    //console.log("active:"+ this.activePath)
  },

  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      //console.log(res);
    },
    //点击按钮,折叠/展开左侧菜单
    toggleClick() {
        this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
        window.sessionStorage.setItem('activePath', activePath) 
        this.activePath = activePath
    }
  },
};
</script>
<style scoped>

#logo {
  height: 40px;
  width: 40px;
  border-radius: 50%;
  padding-right: 10px;
}
.iconfont {
    font-size: 20px;
    margin-right: 10px;
}
.title {
  height: 100%;
  display: flex;
  align-content: center;
}
.title span {
  line-height: 40px;
}
.el-container {
  height: 100%;
}
.el-header {
  padding: 10px;
  background-color: #363d40;
  color: white;
  display: flex;
  justify-content: space-between;
  font-weight: 400;
  font-size: 24px;
  align-items: center;
}
.el-aside {
  background-color: #313743;
  color: white;
}
.el-menu {
    border-right: none;
}
.el-menu-item {
  width: 210px;
}
.el-main {
  background-color: #e9edf1;
}

.toggle-button {
    background-color: #484f64;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.15em;
    cursor:pointer;
}
</style>